package com.ruoyi.business.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.common.exception.ServerExceptionEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MoneyUtil;
import lombok.Data;

import java.io.Serializable;

//@ApiModel
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class OrderResp implements Serializable {

    private static final long serialVersionUID = 5215412336395097751L;

    //@ApiModelProperty(value = "通道类型")
    private String channelType;
    //@ApiModelProperty(value = "通道名称")
    private String channel;
    //@ApiModelProperty(value = "订单金额。单位：元。精确小数点后2位。例：1030.00")
    private String money;
    //@ApiModelProperty(value = "用户实际需要支付的金额。单位：元。精确小数点后2位。例：1021.16")
    private String realMoney;
    //@ApiModelProperty(value = "平台返回的订单号")
    private String tradeNo;
    //@ApiModelProperty(value = "商户自己的唯一订单号")
    private String outTradeNo;
    //@ApiModelProperty(value = "订单状态：1待支付，2支付成功，3已关闭")
    private int status;
    //@ApiModelProperty(value = "支付地址。商户需要展示该地址给付款用户")
    private String payUrl;
    //@ApiModelProperty(value = "付款二维码图片地址。")
    private String qrcodeUrl;
    //@ApiModelProperty(value = "付款二维码图片内容。")
    private String qrcodeContent;
    //@ApiModelProperty(value = "是否支付成功")
    private int paySucc;
    //@ApiModelProperty(value = "是否回调成功")
    private int notifySucc;
    //@ApiModelProperty(value = "订单有效截止时间yyyy-MM-dd HH:mm:ss，超出无法支付。默认5分钟")
    private String validTime;
    //@ApiModelProperty(value = "订单有效截止时间戳（毫秒），超出无法支付。默认5分钟")
    private String validTimeMills;

    private String receiverRealName;
    private String receiverNickname;
    private String receiverUserAvatar;

    private String receiptMoney;

    private String receiverAliUid;
    private String recevierAliLoginId;
    private String recevierAliZZZKL;
    private String nid;
    private String wechatPhone;



    public static OrderResp getOrder(TOrderInfo orderInfo){
        if(orderInfo == null){
            throw new ServiceException(ServerExceptionEnum.NOTEXIST_ORDER.getMessage());
        }
        OrderResp orderResp = new OrderResp();
        orderResp.setChannel(orderInfo.getChannel());
        orderResp.setChannelType(orderInfo.getChannelType());
        orderResp.setMoney(MoneyUtil.fenToYuan(orderInfo.getMoney().toString()));
        orderResp.setRealMoney(MoneyUtil.fenToYuan(orderInfo.getRealMoney().toString()));
        orderResp.setReceiptMoney(MoneyUtil.fenToYuan(orderInfo.getReceiptMoney().toString()));
        orderResp.setNotifySucc(orderInfo.getNotifySucc());
        orderResp.setPaySucc(orderInfo.getPaySucc());
        orderResp.setStatus(orderInfo.getStatus());
        orderResp.setOutTradeNo(orderInfo.getOutTradeNo());
        orderResp.setTradeNo(orderInfo.getId().toString());
        orderResp.setPayUrl(orderInfo.getPayUrl());
        orderResp.setValidTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",orderInfo.getValidTime()));
        orderResp.setQrcodeUrl(orderInfo.getQrcodeUrl());
        orderResp.setQrcodeContent(orderInfo.getQrcodeContent());
        orderResp.setValidTimeMills(String.valueOf(orderInfo.getValidTime().getTime()));
//        Channel orderChannel = Channel.getChannel(orderInfo.getChannel());
//        switch (orderChannel){
//            case WECHAT_PHONE_ZZ:
//                orderResp.setWechatPhone(orderInfo.getChannelAccount());
//                break;
//            default:
//                break;
//        }
//        if( orderInfo.getQrcodeTxt()!=null){
//            Gson gson = new Gson();
//
//            switch (orderChannel.getType()){
//                case ALIPAY:
//                    Map<String,String> alipayMap = gson.fromJson(orderInfo.getQrcodeTxt(),Map.class);
//                    if(alipayMap != null){
//                        orderResp.setReceiverRealName(alipayMap.get("realName"));
//                        orderResp.setReceiverNickname(alipayMap.get("nickname"));
//                        orderResp.setReceiverUserAvatar(alipayMap.get("userAvatar"));
//
//                        orderResp.setReceiverAliUid(alipayMap.get("alipayUserId"));
//                        orderResp.setRecevierAliLoginId(alipayMap.get("alipayLoginId"));
//
//                        orderResp.setRecevierAliZZZKL(alipayMap.get("zzzkl"));
//                    }
//
//                    break;
//                case WECHAT:
//                    Map<String,String> wechatMap = gson.fromJson(orderInfo.getQrcodeTxt(),Map.class);
//                    if(wechatMap != null){
//                        orderResp.setReceiverRealName(wechatMap.get("realName"));
//                    }
//                    break;
//                default:
//                    break;
//            }
//        }

        return orderResp;
    }
}

